/*******************************************************************************
Kakwani and Suits indices.
Last edited: ST, 2/5/2024
*******************************************************************************/

clear all
cd "V:\sthorpe\Rents\Distributional Indices"

*set trace on
*set tracedepth 1

use corpexttm22_updated.dta, clear
* negative ECI values are not used to calculate index
drop if eci < 0

/*
replace corpliab = corpliab / fsize 
replace eci = eci / fsize 
replace tabwt = tabwt * fsize 
*/


**# STEP ONE: CALCULATE INDEX AND CIS 
* calculate index 
program define calculate_index, rclass
args income liability index_type
g posttax = `income' - `liability'
progres `income' posttax [aw=tabwt]
if "`index_type'" == "suits" {
return scalar index_value = r(Suits)
}

if "`index_type'" == "kakwani" {
return scalar index_value = r(Kakwani)
}

drop posttax
 
end 

local corpliabs "corpliab cit_a30l cit_a30lt25 cit_a30lt50 cit_a30ldob cit_a30lb2_3 cit_a50l cit_a50lt25 cit_a50lt50 cit_a50ldob cit_a50lb2_3 cit_a80l cit_a80lt25 cit_a80lt50 cit_a80ldob cit_a80lb2_3 cit_a100lt25 cit_a100lt50 cit_a100ldob cit_a100lb2_3 cit_b20 cit_b40 cit_b80 cit_b100 cit_b00"



* run bootstrap
foreach x in `corpliabs' {
	foreach y in suits kakwani  {
		putexcel set "bootstrapped_values_clean.xlsx", modify sheet("`x'")
		bootstrap r(index_value), seed(10001) reps(10000): calculate_index eci `x' `y'
		estat bootstrap, all 
		putexcel A1 = "Index Name"
		putexcel A2 = "Estimated Index Value"
		putexcel A3 = "CI - Lower"
		putexcel A4 = "CI - Upper"
		if "`y'" == "suits" {
			putexcel B1 = "Suits Index"
			putexcel B2 = matrix(e(b))
			putexcel B3 = matrix(e(ci_normal))
		}
		else {
			putexcel C1 = "Kakwani Index"
			putexcel C2 = matrix(e(b))
			putexcel C3 = matrix(e(ci_normal))
		}

	}
	
}


**# STEP TWO: CALCULATE DIFFERENCE FOR TWO TAX REGIMES 
program define calculate_tvalue, rclass 
args income liability_1 liability_2 index_type 
g posttax_1 = `income' - `liability_1'
g posttax_2 = `income' - `liability_2'
progres `income' posttax_1 [aw=tabwt]
if "`index_type'" == "suits" {
local index_value_1 = r(Suits)
}

if "`index_type'" == "kakwani" {
local index_value_1 = r(Kakwani)
}
progres `income' posttax_2 [aw=tabwt]
if "`index_type'" == "suits" {
local index_value_2 = r(Suits)
}

if "`index_type'" == "kakwani" {
local index_value_2 = r(Kakwani)
}

return scalar index_difference = `index_value_1' - `index_value_2'
drop posttax_*
end 


* run bootstrap 

local corpliabs "corpliab cit_a30l cit_a30lt25 cit_a30lt50 cit_a30ldob cit_a30lb2_3 cit_a50l cit_a50lt25 cit_a50lt50 cit_a50ldob cit_a50lb2_3 cit_a80l cit_a80lt25 cit_a80lt50 cit_a80ldob cit_a80lb2_3 cit_a100lt25 cit_a100lt50 cit_a100ldob cit_a100lb2_3 cit_b20 cit_b40 cit_b80 cit_b100 cit_b00"


foreach x in `corpliabs' {
	foreach y in suits kakwani  {
		putexcel set "bootstrapped_values_clean.xlsx", modify sheet("`x'")
		bootstrap r(index_difference), seed(10001) reps(10000): calculate_tvalue eci corpliab `x' `y'
		
		matrix b = r(table)
		scalar p_1 = b[4,1]
		if "`y'" == "suits" {
			putexcel D2 = p_1
		}
		else {
			putexcel E2 = p_1
		}
		
		matrix drop _all
		scalar drop _all 
		
	}
	
}





